package org.xfy.ymcc.tasks;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.xfy.common.constant.KillConst;
import org.xfy.common.constant.RConst;
import org.xfy.ymcc.domain.KillActivity;
import org.xfy.ymcc.domain.KillCourse;
import org.xfy.ymcc.service.IKillActivityService;
import org.xfy.ymcc.service.IKillCourseService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TimedTasks {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private IKillActivityService killActivityService;
    @Autowired
    private IKillCourseService killCourseService;

    //每天凌晨1点执行一次：0 0 23 * * ?
    //每天晚上，12点，发布秒杀课程到Redis
    @Scheduled(cron = "0/5 * * * * ?")
    @Transactional
    public void publishKillCourse2Redis() {
        //得到今天需要发布的活动
        Date date = new Date();
        List<KillActivity> list = killActivityService.list(new LambdaQueryWrapper<KillActivity>()
                .eq(KillActivity::getPublishStatus, RConst.Num.INT_0)
                .le(KillActivity::getBeginTime, date)
                .ge(KillActivity::getEndTime, date));
        if (list.size() == 0) {
            return;
        }
        //发布到Redis中 需要清空里面的KILL_STORE库存预热数据  KEY_KILL_COURSE的hash结构数据
        //发布到Redis中
        Map<String, KillCourse> allMap = new HashMap<>();   //秒杀课程id为key，秒杀课程对象为值
        list.forEach(killActivity -> {
            //找到所有参加活动的课程
            List<KillCourse> courseLists = killCourseService.list(new LambdaQueryWrapper<KillCourse>()
                    .eq(KillCourse::getPublishStatus, RConst.Num.INT_0)
                    .eq(KillCourse::getActivityId, killActivity.getId()));
            courseLists.forEach(killCourse -> {
                //库存预热  KILL_STORE:27  ->  20
                redisTemplate.opsForValue().set(KillConst.KILL_STORE + ":" + killCourse.getId(), killCourse.getKillCount());
                //课程预热
                allMap.put(killCourse.getId().toString(), killCourse);
                //修改数据
                killCourse.setPublishTime(date);
                killCourse.setPublishStatus(RConst.Num.INT_1);
            });
            killCourseService.updateBatchById(courseLists);
            //修改数据
            killActivity.setPublishTime(date);
            killActivity.setPublishStatus(RConst.Num.INT_1);

        });
        //批量添加到Redis的hash结构中
        //redisTemplate.opsForHash().putAll(KillCons.KEY_KILL_COURSE, allMap);
        BoundHashOperations<String, Object, Object> hashOps = redisTemplate.boundHashOps(KillConst.KEY_KILL_COURSE);
        hashOps.putAll(allMap);
        //修改状态
        killActivityService.updateBatchById(list);
    }
}
